var SdsCi = {
    sdsCiData: {},
    layerIndex: -1
};

/**
 * 获取对话框中的数据
 *
 * param key 数据的名称
 * param val 数据的具体值
 */
SdsCi.get = function (key, type) {

    var res = "";
    switch (type) {
        case "checkbox":
            $("input[type='checkbox'][name=" + key + "]:checked").each(function () {
                //  选中复选框中的值
                res += $(this).val() + ",";
            });
            res = res.substring(0, res.length - 1)
            break;
        case "radio":
            var obj = $("input[type='radio'][name=" + key + "]:checked");
            res = obj.val();
            break;
        default:
            res = $("input[name=" + key + "]").val();
            break;
    }
    return res;
};

/**
 * 收集数据
 */
SdsCi.collectData = function () {
    $(".item").each(function () {
        var key = $(this)[0].id;
        key = key.split("_")[0];    // 为了解决两个禁忌症前置项同为一个项目，单选按钮互斥效果紊乱

        if (SdsCi.sdsCiData[key] != "y") {
            SdsCi.sdsCiData[key] = SdsCi.get($(this)[0].id, $(this).attr("ciType"));
        }
    });
};

/**
 * 关闭弹窗
 */
SdsCi.close = function () {
    parent.postMessage("close","*");
}

/**
 * 保存禁忌症
 */
SdsCi.save = function () {
    this.collectData();
    //提交信息
    var ajax = new $ax("/api/sdstool/saveCIInfo", function (data) {
        layer.msg("保存成功!");
        parent.postMessage("save","*");
    }, function (data) {
        layer.error("保存失败!");
    });
    var params = {};
    params.patientId = $('#patientId').val();
    params.entityId = $('#entityId').val();
    params.inCaseId = $('#inCaseId').val();
    params.modifyUserId = $('#modifyUserId').val();
    params.modifyUserName = $('#modifyUserName').val();
    params.recordId = $('#recordId').val();
    params.eventId = $('#eventId').val();
    params.sdsCiData = JSON.stringify(this.sdsCiData);
    ajax.set(params);
    ajax.start();
}

/**
 * 前置项选择
 * param that
 */
SdsCi.switchChange = function (){
    var flg = $(this).switchbox('getValue');
    if (flg) {
        $("."+this.id).show();
    } else {
        $("."+this.id).hide();
    }
}

/**
 * 前置项选择控制具体选择项的显隐
 * param that
 */
function preChecked(that){
    var val = $(that).val();
    var selItem = that.parentNode.parentNode.parentNode.getElementsByClassName("sel");
    var selItemId = selItem[0].id;
    if (val == "y") {
        $("#"+selItemId).show();
    } else {
        $("#"+selItemId).hide();
    }
}

/**
 * 改变前置项的值及提醒填写自填项
 * param that
 */
function selChecked(that){
    var itemId = $(that).attr('name');
    var res = SdsCi.get(itemId, $('#'+itemId).attr("ciType"));
    var preItem = that.parentNode.parentNode.parentNode.getElementsByClassName("pre");
    var preItemId = preItem[0].id;
    if(typeof res !== 'undefined'||res!==""){
        $('[name='+preItemId+'][value="y"]').radio('setValue', true);
    } else {
        $('[name='+preItemId+'][value="n"]').radio('setValue', true);
    }
    var inpItem = that.parentNode.parentNode.getElementsByClassName("inp");
    var inpItemId = inpItem[0].id;
    if ($(that).val() == 'oth') {
        if($(that).is(":checked")) {
            $("#" + inpItemId).show();
            layer.msg("请在下方输入框内填写具体禁忌项!");
        } else {
            $("#"+inpItemId).hide();
        }
    };
}

$(function () {
    var ciStr = "";
    // 获取阶段信息
    var ajax = new $ax("/api/sdstool/getCIInfo", function (data) {
        var l = data.length;
        if (!l > 0) {
            ciStr = "<div><span style=\"color: red; font-size: large\">该病种暂无维护禁忌症</span></div>";
        } else {
            for (let i = 0; i < l; i++) {
                var ci = data[i];
                var ciName = ci.ciName;
                var preItem = ci.preItem;
                var selItem = ci.selItem;
                var inpItem = ci.inpItem;
                ciStr += "<div class=\"cisub\" id=\"${ci.eventId}\">\n" +
                    "            <div class=\"J_nav\" style=\"font-weight: bolder\" name=\"title\">\n" +
                    "                <span>" + (i+1) + "." + ciName + "</span>\n" +
                    "            </div>";
                var displayFlg = "display:none";
                if(preItem.itemId){
                    var preClazz = 'radio';
                    var preItemId = preItem.itemId;
                    var preValArr = preItem.itemResult.split(",");
                    var preItems = preItem.ciItems;
                    ciStr += "<div class=\"item pre\" id=\"" + preItemId + "_"+ i  + "\" ciType=\"" + preClazz + "\">"
                    for (let j = 0; j < preItems.length; j++) {
                        var citem = preItems[j];
                        ciStr += "<div name=\"content\" style=\"display: inline-block\">";
                        var preChkFlg = "";
                        for (let k = 0; k < preValArr.length; k++) {
                            let val = preValArr[k];
                            if(val == citem.code){
                                preChkFlg = 'checked="checked"';
                                if (val == 'y') {
                                    displayFlg = '';
                                }
                                break;
                            }
                        }
                        ciStr += "<input class=\"hisui-" + preClazz + "\" type=\"" + preClazz + "\" name=\"" + preItemId + "_"+ i  +  "\" " +
                            "id=\"" + citem.id + "\" group=\"" + citem.group + "\" label=\"" + citem.desc + "\" value=\"" + citem.code + "\""+ preChkFlg +">\n";
                        ciStr += "</div>";
                    }
                    ciStr += "</div>";
                } else {
                    displayFlg = '';
                }
                var inpFlg  = 'display: none';
                if(selItem.itemId){
                    var selClazz = (selItem.itemType=='CheckGroup')?'checkbox':'radio';
                    var selItemId = selItem.itemId;
                    var selValArr = selItem.itemResult.split(",");
                    var selItems = selItem.ciItems;
                    ciStr += "<div class=\"item sel ci_" + i + "\" id=\"" + selItemId + "\" ciType=\"" + selClazz + "\" style=\""+displayFlg+"\">";
                    for (let j = 0; j < selItems.length; j++) {
                        var citem = selItems[j];
                        ciStr += "<div name=\"content\">";
                        var selChkFlg = "";
                        for (let k = 0; k < selValArr.length; k++) {
                            let val = selValArr[k];
                            if(val == 'oth'){
                                inpFlg = "";
                            }
                            if(val == citem.code){
                                selChkFlg = 'checked="checked"';
                                break;
                            }
                        }
                        ciStr += "<input class=\"hisui-" + selClazz + "\" type=\"" + selClazz + "\" name=\"" + selItemId + "\" " +
                            "id=\"" + citem.id + "\" group=\"" + citem.group + "\" label=\"" + citem.desc + "\" value=\"" + citem.code + "\""+ selChkFlg +">\n";
                        ciStr += "</div>";
                    }
                    if(inpItem.itemId){
                        ciStr += "<div class=\"item inp\" id=\"" + inpItem.itemId + "\" style=\"" + inpFlg + "\" >";
                        ciStr += "  <input class=\"textbox\" type=\"text\" name=\"" + inpItem.itemId + "\" value=\"" + inpItem.itemResult + "\">";
                        ciStr += "</div>";
                    }
                    ciStr += "</div>";
                }
                ciStr += "</div>";
            }
        }
        $("#ciDiv").append(ciStr);
        $.parser.parse("#ciDiv");
    });
    var params = {};
    params.patientId = $('#patientId').val();
    params.entityId = $('#entityId').val();
    params.inCaseId = $('#inCaseId').val();
    params.docCode = $('#docCode').val();
    params.recordId = $('#recordId').val();
    params.eventId = $('#eventId').val();
    ajax.set(params);
    ajax.start();

    $('.pre input:radio').radio({
        onChecked: function (){
            preChecked(this);
        }
    });

    $('.sel input:checkbox').checkbox({
        onCheckChange: function () {
            selChecked(this);
        }
    });
    $('.sel input:radio').radio({
        onCheckChange: function (){
            selChecked(this);
        }
    });
});